<script setup>
import {onMounted, ref} from "vue";
import {useRouter} from "vue-router"
import {get} from "../../util/request.js";
import {useStore} from "../../pinia/index.js";

const router = useRouter();
const store = useStore();
const chosenAddressId = ref('1');
const list = ref([
  // {
  //   id: '1',
  //   name: '张三',
  //   tel: '13000000000',
  //   address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',
  //   isDefault: true,
  // },
]);
const disabledList = [
  // {
  //   id: '3',
  //   name: '王五',
  //   tel: '1320000000',
  //   address: '浙江省杭州市滨江区江南大道 15 号',
  // },
];


onMounted(()=>{
  get("/address/getAddressByUserId").then(res=>{
      if(res.code==0){
        // debugger
        console.log(list)

        for (var i = 0;i<res.data.length;i++){
          if(res.data[i].isDefault==1){
            list.value.push({id:''+res.data[i].addressId,name:res.data[i].name,tel:res.data[i].phoneNumber,address:res.data[i].city+res.data[i].district+res.data[i].province+res.data[i].detailAddress,isDefault:true})
          }else {
            list.value.push({
              id: '' + res.data[i].addressId,
              name: res.data[i].name,
              tel: res.data[i].phoneNumber,
              address: res.data[i].city + res.data[i].district + res.data[i].province + res.data[i].detailAddress
            })
          }
        }

        console.log(list)

        // disabledList.value.id=res.data.addressId
        // debugger
        // disabledList.value.name=res.data.name
        // disabledList.value.tel=res.data.phoneNumber
        // disabledList.value.address=res.data.city+res.data.district+res.data.privice
        //
        // addressList.values=res.data;
      }else{

        if (res.msg=="请求操作失败"){
          if(res.data=="用户未登录"){
            router.push("/login")
          }

        }
      }
  })
})
const onAdd = () =>{
  store.setCurrentAddress({})

  router.push({path:"/addressEdit",query:{title:'新增地址'}});
};
const onEdit = (item, index) => {
  console.log(item)
  console.log(index)

  store.setCurrentAddress(item)
  router.push({path:"/addressEdit",query:{title:'编辑地址'}});
};

const onClickLeft = () => history.back();

const onSelect = (item,index) =>{
  store.setCurrentAddress(item);
}
</script>

<template>
  <van-nav-bar
      title="我的收货地址"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
  />
  <van-address-list
      v-model="chosenAddressId"
      :list="list"
      :disabled-list="disabledList"
      disabled-text="以下地址超出配送范围"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
      @select="onSelect"
  />

</template>

<style scoped>

</style>